package classes;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class HomeworkManagerLecturer {

	static Statement statement;
	public static String HOMEWORKMANAGERLECTURER = "homeworkManagerLecturer";

	public HomeworkManagerLecturer(Statement statement) {
		HomeworkManagerLecturer.statement = statement;
	}

	public void createHomework(String homeworkName, int groupID,
			String startTime, String endTime, String file) {
		try {
			statement
					.executeUpdate("insert into homeworkforlecturers values (0, '"
							+ homeworkName
							+ "',"
							+ groupID
							+ ",'"
							+ startTime
							+ "', '" + endTime + "', '" + file + "');");
			ArrayList<Integer> arr = new ArrayList<Integer>();
			arr = GroupManager.getStundetsOfGroup(groupID);
			ResultSet rs = statement
					.executeQuery("select homework_id from homeworkforlecturers;");
			rs.last();
			int homework_id = rs.getInt("homework_id");
			rs.close();
			for (int i = 0; i < arr.size(); i++) {
				statement.executeUpdate(
						"insert into homeworkforstudents values(" + homework_id
								+ ",'" + homeworkName + "\'," + arr.get(i)
								+ "," + "''" + "," + "''" + ","+ "''" + ","+ null + ","+ "''" + ","+ "''" + ");");
			}
		} catch (SQLException e) {
			System.out.println("homework for lecturers not created");
			e.printStackTrace();
		}
	}
	public void deleteHomework(int homework_id){
		try {
			statement.executeUpdate("delete from homeworkforlecturers where homework_id=" + homework_id);
			statement.executeUpdate("delete from homeworkforstudents where homework_id=" + homework_id);		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @param groupID
	 * @return returns all {@link HomeworkLecturer} if there is any, null
	 *         otherwise
	 */
	public HomeworkLecturer[] getHomeworks(int groupID) {
		HomeworkLecturer[] homeworks = null;
		try {
			ResultSet set = statement
					.executeQuery("select * from homeworkforlecturers where group_id = "
							+ groupID);
			set.last();
			homeworks = new HomeworkLecturer[set.getRow()];
			set.beforeFirst();
			for (int i = 0; set.next(); i++) {
				HomeworkLecturer temp = new HomeworkLecturer(set.getInt(1),
						set.getString(2), set.getInt(3), set.getString(4),
						set.getString(5), set.getString(6));
				homeworks[i] = temp;
			}
		} catch (SQLException e) {
			System.out.println("cant get homeworks from homeWorklecturer");
			e.printStackTrace();
		}
		return homeworks;
	}

	/**
	 * updates homeworkLecturer table. updates columns with given value on given
	 * homeworkID's row
	 * 
	 * @param homeworkID
	 * @param homeworkName
	 * @param startTime
	 * @param endTime
	 * @param file
	 */
	public static void updateHomework(int homeworkID, String homeworkName,
			int groupID, String startTime, String endTime, String file) {
		try {
			statement.executeUpdate("update homeWorkForLecturers set"
					+ " homework_name = '" + homeworkName + "', group_id = "
					+ groupID + ", start_time = '" + startTime
					+ "', end_time = '" + endTime + "', filename_lecturer = '"
					+ file + "' where homework_id = " + homeworkID);
		} catch (SQLException e) {
			System.out.println("cant update homework for homeWorklecturer");
			e.printStackTrace();
		}
	}

}
